package com.rohoo.products.dao.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.rohoo.framework.dao.BaseDAO;
import com.rohoo.framework.dao.DAORowMapper;
import com.rohoo.framework.dao.DataPackage;
import com.rohoo.framework.util.StringUtil;
import com.rohoo.products.bo.Products;
import com.rohoo.products.dao.ProductsDAO;
import com.rohoo.products.dto.ProductsDTO;
import com.wondersgroup.framework.persistence.DAOException;

public class ProductsDAOImpl extends BaseDAO implements ProductsDAO {

	Logger log = Logger.getLogger(this.getClass());

	public void addProducts(Products products) throws Exception {
		// TODO Auto-generated method stub
		String sql = new String("");
		try {
			sql = "insert into Products(content," + "parents_id,pictures_id,"
					+ "news_id,create_time,update_time) values " + "'"
					+ StringUtil.filterForSQL(products.getContent()) + "',"
					+ products.getParents_id() + products.getPictures_id()
					+ products.getNews_id() + "sysdate," + "sysdate)";
			this.getJdbcTemplate().update(sql);
		} catch (Exception e) {
			System.out.println(e);
			log.error("addProducts Error! sql=" + sql, e);
		}
	}

	public void updateProducts(ProductsDTO productsDTO) throws DAOException {
		// TODO Auto-generated method stub
		String sql = new String("");
		try {
			sql = "update Products set " + productsDTO.getContent() == null ? ""
					: productsDTO.getContent().equals("") ? "": ("content=" + productsDTO.getContent()+",")
					+ productsDTO.getParents_id() == null ? "": ("parents_id="+ productsDTO.getParents_id()+",")
					+ productsDTO.getPictures_id() == null ? "": ("pictures_id="+ productsDTO.getPictures_id()+",")
					+ productsDTO.getNews_id() == null ? "": ("news_id="+ productsDTO.getNews_id()+",")
					+"update_time=sysdate " +
					"where products_id="+productsDTO.getProducts_id();
			this.getJdbcTemplate().update(sql);
		} catch (Exception e) {
			System.out.println(e);
			log.error("updateProducts Error,sql=" + sql, e);
		}
	}
	
	public DataPackage queryProducts(ProductsDTO dto)throws Exception{
		StringBuffer qrySQL=new StringBuffer();
		StringBuffer countSQL=new StringBuffer();
		StringBuffer condition=new StringBuffer(" where 1=1 ");
		
		String qry_products_name=dto.getQry_products_name();
		String qry_content=dto.getContent();
		
		if(StringUtils.isNotEmpty(qry_products_name)){
			condition.append(" and products_name like '%"+StringUtil.filterForSQL(qry_products_name)+"%'");
		}
		if(StringUtils.isNotEmpty(qry_content)){
			condition.append(" and content like '%"+StringUtil.filterForSQL(qry_content)+"%'");
		}
		
		countSQL=countSQL.append("select count(*) from products ").append(condition);
		
		qrySQL=qrySQL.append("select t.PRODUCTS_ID,t.products_name,t.CONTENT,t.parents_id,t.pictures_id,t.news_id")
			.append("t.create_time.t.upload_time,t.delte_flag,")
		.append("trim(to_char(t.create_time,'yyyy-MM-dd hh24:mi:ss')) as create_time_str")
		.append("trim(to_char(t.upload_time,'yyyy-MM-dd hh25:mi:ss')) as upload_time_str ")
		.append(" from products t").append(condition)
		.append(" order by t.create_time desc");
		return this.getPage(qrySQL.toString(),countSQL.toString(),Products.class,dto.get_currpage(),dto.get_pagelines());
	}

	public void deleteProducts(ProductsDTO productsDTO) throws DAOException {
		// TODO Auto-generated method stub
		String sql="";
		try{
			sql="update Products set deleteflag="+productsDTO.getDelete_flag()+
					" where products_id="+productsDTO.getProducts_id();
			this.getJdbcTemplate().update(sql);
		}catch(Exception e){
			System.out.println(e);
			log.error("deleteProducts Error,sql="+sql,e);
		}
	}
	
	public Products getProducts(Integer products_id)throws Exception{
		Products products=new Products();
		try{
			String sql="selecet t.*,trim(to_char(t.create_time,'yyyy-MM-dd hh24:mi:ss')) as create_time_str," +
					"trim(to_char(t.create_time,'yyyy-MM-dd hh24:mi:ss')) as upload_time_str from products t " +
					"where t.products_id="+products_id;
			products=(Products)this.getJdbcTemplate().queryForObject(sql, new DAORowMapper(Products.class));
		}catch(Exception e){
			products=null;
		}
		return products;
	}
}
